package com.linsen.oss.controller;

import cn.hutool.extra.servlet.ServletUtil;
import com.linsen.core.common.util.Result;
import com.linsen.oss.provider.vo.UploadFileData;
import com.linsen.oss.service.FileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/oss")
@RequiredArgsConstructor
@Api(tags = "OSS")
public class OSSController {

	private final FileUploadService fileUploadService;

	@ApiOperation(value = "文件上传-返回文件地址",response = String.class)
	@ApiImplicitParam(name = "file", value = "表单文件MultipartFile", required = true, dataType = "file")
	@PostMapping("/file/upload")
	public Result uploadFile(@ApiIgnore @RequestParam MultipartFile file, HttpServletRequest request) {
		log.info("upload file client ip: {}", ServletUtil.getClientIP(request));
		return Result.data(this.fileUploadService.uploadFile(file));
	}

	@ApiOperation(value = "文件上传-返回文件详情",response = UploadFileData.class)
	@ApiImplicitParam(name = "file", value = "表单文件MultipartFile", required = true, dataType = "file")
	@PostMapping("/file/info/upload")
	public Result uploadFileInfo(@ApiIgnore @RequestParam MultipartFile file, HttpServletRequest request) throws Exception {
		log.info("upload file client ip: {}", ServletUtil.getClientIP(request));
		return Result.data(this.fileUploadService.uploadFileInfo(file));
	}
}
